import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { Http } from '@angular/http';
import { LeftSidebarService } from '../sidebar/left-sidebar-service';
import { BaseComponet } from '../BaseComponet';
import { Subscription } from 'rxjs';

declare var $: any;
@Component({
    selector: 'app-integration-browse',
    templateUrl: './integration-browse.component.html',
    styleUrls: ['./integration-browse.component.css']
})
export class IntegrationBrowseComponent extends BaseComponet implements OnInit {
    public isOnShow = true;
    public menus: any[];
    public minPageNo: number;
    public minPageSize: number;
    public minOrderBy: String = '';
    public minOrderType: String = '';
    public minSearchKeywords: String = '';
    public minTotal: number;
    public minTotalPages: number;
    public minPageUserId: String;
    public userJobRolesUserName: String;
    public jobUserRoles: any[];
    public checkedUserJobRoleIds: any[];
    public checkedAllUserJobRoles: boolean;
    public showUserJobRoles = false;
    public showDeleteUserJobRoles = false;
    public menuChangeSubscription: Subscription;
    constructor(
        public routerInfo: ActivatedRoute,
        public http: Http,
        public router: Router,
        public leftSidebarService: LeftSidebarService
    ) {
        super();
    }
    switchButtons() {
        console.log('switchButtons()');
        this.menus = JSON.parse(sessionStorage.getItem('menus'));
        if (
            this.menus.indexOf('manager:job.back.manage-role-jobs.delete') > -1
        ) {
            this.showDeleteUserJobRoles = true;
        } else {
            this.showDeleteUserJobRoles = false;
        }
    }
    ngOnDestroy() {
        this.isOnShow = false;
        this.menuChangeSubscription.unsubscribe();
    }
    ngOnInit() {
        this.hideLoading();
        this.switchButtons();
        this.menuChangeSubscription = this.leftSidebarService.menusChange.subscribe(
            command => {
                this.switchButtons();
            }
        );
        $('[data-toggle="popover"]').popover();
        // $(document).on('show.bs.modal', '.modal', function () {
        //   $(this).draggable();
        //   $(this).css('overflow-y', 'scroll');
        //   // 防止出现滚动条，出现的话，你会把滚动条一起拖着走的
        // });
        this.userJobRolesUserName = '账号机构职位角色一览表';
        this.minPageNo = 1;
        this.minPageSize = parseInt(localStorage.getItem('minPageSize'), 10);
        if (this.minPageSize == null || isNaN(this.minPageSize)) {
            this.minPageSize = 10;
        }
        this.displayMinData();
    }
    setMinPageSize(pageSize) {
        this.minPageNo = 1;
        this.minPageSize = pageSize;
        localStorage.setItem('minPageSize', pageSize);
        this.displayMinData();
    }
    goToMinPage(pageNo) {
        this.minPageNo = pageNo;
        this.displayMinData();
    }
    clearMinKeywords() {
        this.minSearchKeywords = '';
    }
    displayMinData() {
        this.showLoading();
        this.http
            .get(
                '/user/back/job/page/search-' +
                    this.minSearchKeywords +
                    '/order-by-' +
                    this.minOrderBy +
                    '/order-type-' +
                    this.minOrderType +
                    '/page-no/' +
                    this.minPageNo +
                    '/page-size/' +
                    this.minPageSize
            )
            .map(result => result.json())
            .subscribe(
                result => {
                    if (this.isOnShow) {
                        this.hideLoading();
                    }
                    this.minPageNo = result.pageNo;
                    this.minPageSize = result.pageSize;
                    this.minTotal = result.total;
                    this.jobUserRoles = result.list;
                    this.minTotalPages = result.totalPages;
                    this.minOrderBy = result.orderBy;
                    this.minOrderType = result.orderType;
                },
                error => {}
            );
    }
    minSort(orderBy) {
        if (this.minOrderBy === orderBy) {
            if (this.minOrderType === 'asc') {
                this.minOrderType = 'desc';
            } else {
                this.minOrderType = 'asc';
            }
        } else {
            this.minOrderBy = orderBy;
        }
        this.displayMinData();
    }
    checkAllUserJobRoles() {
        if (this.checkedAllUserJobRoles === false) {
            this.checkedAllUserJobRoles = true;
            this.checkedUserJobRoleIds = [];
            for (let i = 0; i < this.jobUserRoles.length; i++) {
                this.checkedUserJobRoleIds.push(
                    this.jobUserRoles[i].otherResults.id
                );
            }
            $('.job-list').prop('checked', true);
        } else {
            this.checkedAllUserJobRoles = false;
            this.checkedUserJobRoleIds = [];
            $('.job-list').prop('checked', false);
        }
    }
    checkUserJobRole() {
        this.checkedUserJobRoleIds = [];
        if ($('.job-list').length === $('.job-list:checked').length) {
            this.checkedAllUserJobRoles = true;
        } else {
            this.checkedAllUserJobRoles = false;
        }
        const checkedUserJobRoleIds = this.checkedUserJobRoleIds;
        $('.job-list:checked').each(function() {
            checkedUserJobRoleIds.push($(this).val());
        });
    }
    toDeleteUserJobRoles() {
        if (this.checkedUserJobRoleIds.length <= 0) {
            alert('请先选中需要删除的角色！');
            return;
        }
        if (confirm('确定删除选中的角色吗？')) {
            this.http
                .delete(
                    '/user/back/job/by-ids/' +
                        this.checkedUserJobRoleIds.join('-')
                )
                .map(result => result.json())
                .subscribe(
                    result => {
                        alert(result.message);
                        this.checkedAllUserJobRoles = false;
                        this.checkedUserJobRoleIds = [];
                        this.displayMinData();
                    },
                    error => {
                        this.displayMinData();
                    }
                );
        }
    }
}
